package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {

    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取页面参数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String searchName = req.getParameter("searchName");
        // 调用业务层对象进行操作
        List<Role> list = roleService.findAll(pageNum, pageSize, searchName);
        PageInfo<Role> roleList = new PageInfo<>(list);
        req.getSession().setAttribute("searchName", searchName);
        req.getSession().setAttribute("page", roleList);
        // 转到展示页面
        req.getRequestDispatcher("/pages/role-list.jsp").forward(req, resp);
    }

    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 封装角色对象
        Role role = BeanUtil.fillBean(req, Role.class);
        // 调用业务层对象进行操作
        roleService.save(role);
        // 跳转到展示页面
        findAll(req, resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取客户端传递的id
        String ids = req.getParameter("ids");
        // 调用业务层对象进行操作
        roleService.delete(ids);
        // 跳转到展示页面
        findAll(req, resp);
    }

    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取客户端传递的id
        String id = req.getParameter("id");
        // 调用业务层对象进行操作
        Role role = roleService.findById(id);
        req.getSession().setAttribute("user", role.getUsers());
        // 跳转到展示用户页面
        req.getRequestDispatcher("/pages/role-user-show.jsp").forward(req, resp);
    }

    protected void toPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取客户端传递的id
        String roleId = req.getParameter("id");
        // 调用业务层对象进行操作
        List<Permission> permissionList = permissionService.findByRole(roleId);
        // 将参数传递到
        req.setAttribute("id", roleId);
        req.setAttribute("permissionList", permissionList);
        // 转发到添加权限页面
        req.getRequestDispatcher("/pages/role-permission-add.jsp").forward(req, resp);
    }

    protected void updatePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String[] permissionId = req.getParameterValues("ids");
        roleService.updatePermission(roleId,permissionId);
        findAll(req, resp);
    }
}
